package vn.edu.ptithcm.pim.validator;

import vn.edu.ptithcm.pim.dom.Project;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class ProjectValidator implements Validator {

	public boolean supports(@SuppressWarnings("rawtypes") Class clazz) {
		return Project.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		Project project = (Project) target;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
				"warning.required.project.name");
//		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "number",
//				"warning.required.project.number");
//		if (project.getNumber() == 0) {
//			errors.rejectValue("number", "warning.project.number.is.zero");
//		}
		if (project.getCustomer().getCustomerid() == 0) {
			errors.rejectValue("customer", "warning.required.project.customer");
		}
		if (project.getGroup().getGroupid() == 0) {
			errors.rejectValue("group", "warning.required.project.group");
		}
		if (project.getProjectStatus().getStatusid() == 0) {
			errors.rejectValue("projectStatus",
					"warning.required.project.status");
		}
		if (project.getStartdate() == null) {
			errors.rejectValue("startdate",
					"warning.required.project.startdate");
		} else if (project.getEnddate() != null) {
			int c = project.getStartdate().compareTo(project.getEnddate());
			if (c > 0) {
				errors.rejectValue("enddate",
						"warning.project.startdate.after.enddate");
			}
		}
	}
}
